package cn.itcast.web;

import cn.itcast.pojo.classsify;
import cn.itcast.service.impl.searchOrClassifyServiceImpl;
import cn.itcast.util.OutputStringController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value="/wxshop/user",produces="text/html;charset=UTF-8")
public class SearchOrClassifyController {
//    @Autowired
//    private DataSource dataSource;
    @Resource
searchOrClassifyServiceImpl searchOrClassifyService;
    @Resource
    OutputStringController osc;

    /**
     *功能：查找所有分类产品
     * @author 欧阳辉
     * @date 2020/3/16
      * @param map
     * @return java.lang.String
     */
    @PostMapping("findAllClassify")
    public String findAllClassify(@RequestBody Map<String,String> map){
        log.debug("查找所有分类产品");
        //返回结果
        String results = "";

        List<Map<String,Object>> classsifys = searchOrClassifyService.findAllClassify();
        if(classsifys.isEmpty()){
            results = osc.resultFailure("no","");
            log.debug("搜索商品或分类商品失败");
        }else{
            results = osc.resultSuccess("yes",classsifys);
            log.debug("搜索商品或分类商品成功");
        }
        return results;
    }

    /**
     *功能：查找某一分类产品
     * @author 欧阳辉
     * @date 2020/3/16
     * @param map
     * @return java.lang.String
     */
    @PostMapping("findOneClassify")
    public String findOneClassify(@RequestBody Map<String,String> map){
        log.debug("查找某一分类产品");
        //返回结果
        String results = "";
        Integer fid = Integer.parseInt(map.get("fid"));

        List<classsify> classsifys = searchOrClassifyService.findOneClassify(fid);
        if(classsifys.isEmpty()){
            results = osc.resultFailure("no","");
            log.debug("搜索商品或分类商品失败");
        }else{
            results = osc.resultSuccess("yes",classsifys);
            log.debug("搜索商品或分类商品成功");
        }
        return results;
    }


    /**
     *功能：查找某一产品
     * @author 欧阳辉
     * @date 2020/3/16
     * @param map
     * @return java.lang.String
     */
    @PostMapping("findOne")
    public String findOne(@RequestBody Map<String,String> map){
        log.debug("查找某一分类产品");
        //返回结果
        String results = "";
        Integer id = Integer.parseInt(map.get("id"));

        classsify classsify = searchOrClassifyService.findOne(id);
        if(classsify==null){
            results = osc.resultFailure("no","");
            log.debug("搜索商品或分类商品失败");
        }else{
            results = osc.resultSuccess("yes",classsify);
            log.debug("搜索商品或分类商品成功");
        }
        return results;
    }

}
